package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;
public class StudentDao implements BaseStudentDao{
   static Student[] stus=new Student[5];
    static{
        Student stu1 = new Student("heima001","张三","23","1998-01-01");
        Student stu2 = new Student("heima002","李三","23","1996-01-01");
        Student stu3 = new Student("heima003","王五","24","1997-01-01");
        stus[0]=stu1;
        stus[1]=stu2;
        stus[2]=stu3;
    }
    public  void deleteStudent(String sid) {
        int index = getIndex(sid);
        //将索引位置使用null进行替换
        stus[index]=null;

    }
   public  int getIndex(String sid){
        int index=-1;
       for (int i = 0; i < stus.length; i++) {
           if(stus[i]!=null&&stus[i].getId().equals(sid)){
               index=i;
               break;
           }
       }
        return index;
   }

    public  void updataStudent(Student student, String sid) {
        int index = getIndex(sid);
        if(index!=-1){
            stus[index]=student;
        }
    }


    public boolean addStudent(Student stu) {
        int index=-1;
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if(student==null){
                index=i;
                break;
            }
        }
        if(index==-1){
           return false;
        }else{
            stus[index]=stu;
            return true;
        }
    }

    public Student[] findAllStudent() {
        return stus;
    }
}
